package com.avecsys.quotemanager.mbeans.supplier;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.icefaces.ace.event.SelectEvent;
import org.icefaces.ace.json.JSONException;
import org.icefaces.ace.json.JSONObject;

import com.avecsys.quotemanager.dao.common.EnumTransactionStatus;
import com.avecsys.quotemanager.exception.BusinessException;
import com.avecsys.quotemanager.mbeans.core.BaseMBean;
import com.avecsys.quotemanager.pojos.SupplierMasterPojo;
import com.avecsys.quotemanager.service.supplier.SupplierMasterService;
import com.avecsys.quotemanager.ui.components.MessageDialog;

public class SupplierMasterListMBean extends BaseMBean implements Serializable {

	private final Log log = LogFactory.getLog(SupplierMasterListMBean.class);

	private List<SupplierMasterPojo> supplierMasterPojoList = new ArrayList<SupplierMasterPojo>();

	private SupplierMasterService supplierMasterService;

	/**
	 * Default Constructor
	 */
	public SupplierMasterListMBean() {
		super();
	}

	/**
	 * @return the supplierMasterPojoList
	 */
	public List<SupplierMasterPojo> getSupplierMasterPojoList() {
		return supplierMasterPojoList;
	}

	/**
	 * @return the supplierMasterService
	 */
	public SupplierMasterService getSupplierMasterService() {
		return supplierMasterService;
	}

	/**
	 * @param supplierMasterPojoList
	 *            the supplierMasterPojoList to set
	 */
	public void setSupplierMasterPojoList(
			List<SupplierMasterPojo> supplierMasterPojoList) {
		this.supplierMasterPojoList = supplierMasterPojoList;
	}

	/**
	 * @param supplierMasterService
	 *            the supplierMasterService to set
	 */
	public void setSupplierMasterService(
			SupplierMasterService supplierMasterService) {
		this.supplierMasterService = supplierMasterService;
	}

	public void init() {
		try {
			setSupplierMasterPojoList(supplierMasterService.getAllSuppliers());
		} catch (BusinessException e) {
			log.error(e);
		}
	}

	/**
	 * 
	 * @param actionEvent
	 */
	public void deleteSupplierMaster(ActionEvent actionEvent) {
		FacesContext faceContext = FacesContext.getCurrentInstance();
		String requestJSON = getMessageDialog().getRequestJSON();
		JSONObject requestJSONObject;
		String supplierId = null;
		try {
			requestJSONObject = new JSONObject(requestJSON);
			supplierId = (String) requestJSONObject.get("supplierId");
			Integer transactionStatus = supplierMasterService
					.deleteSupplierMasterById(new Long(supplierId));
			if (transactionStatus.intValue() == EnumTransactionStatus.SUCESS
					.ordinal()) {
				init();
				FacesMessage facesMessage = new FacesMessage(
						FacesMessage.SEVERITY_INFO,
						"Selected Supplier sucessfully deleted.", null);
				faceContext.addMessage("messageBoardId", facesMessage);
			} else {
				FacesMessage facesMessage = new FacesMessage(
						FacesMessage.SEVERITY_ERROR,
						"Selected Supplier failed to delete.", null);
				faceContext.addMessage("messageBoardId", facesMessage);
			}
		} catch (BusinessException e) {
			log.error(e);
		} catch (Exception e) {
			log.error(e);
		}
		getMessageDialog().hideDialog();
	}

	/**
	 * 
	 * @param actionEvent
	 */
	public void confirmDeleteAction(ActionEvent actionEvent) {
		FacesContext faceContext = FacesContext.getCurrentInstance();
		Map<String, String> paramValues = faceContext.getExternalContext()
				.getRequestParameterMap();
		String supplierId = (String) paramValues.get("supplierId");
		String companyName = (String) paramValues.get("companyName");
		JSONObject requestJSONObject = new JSONObject();
		try {
			requestJSONObject.put("supplierId", supplierId);
			requestJSONObject.put("companyName", companyName);
			getMessageDialog().setRequestJSON(requestJSONObject.toString());
			getMessageDialog().setMessage(
					"Are you sure to delete '" + companyName + "' ?");
		} catch (JSONException e) {
			log.error(e);
		}
		getMessageDialog().showDialog();
	}

	/**
	 * Method to edit the supplier master information
	 * 
	 * @return
	 */
	public String editSupplierMaster() {
		// Call Common routine
		executePreRoutines();

		FacesContext faceContext = FacesContext.getCurrentInstance();
		Map<String, String> paramValues = faceContext.getExternalContext()
				.getRequestParameterMap();
		String supplierId = (String) paramValues.get("supplierId");
		try {
			SupplierMasterPojo supplierMasterPojo = supplierMasterService
					.getSupplierMasterById(new Long(supplierId));
			SupplierMasterMBean supplierMasterMBean = (SupplierMasterMBean) faceContext
					.getApplication()
					.evaluateExpressionGet(faceContext,
							"#{supplierMasterMBean}", SupplierMasterMBean.class);
			supplierMasterMBean.setSupplierMasterPojo(supplierMasterPojo);
			supplierMasterMBean.setScreenModeEdit(true);
		} catch (BusinessException e) {
			log.error(e);
		} catch (Exception e) {
			log.error(e);
		}
		return "success";
	}

	/**
	 * 
	 * @param selectEvent
	 */
	public String showSelectedSupplierUsers(SelectEvent selectEvent) {
		FacesContext faceContext = FacesContext.getCurrentInstance();
		SupplierMasterPojo supplierMasterPojo = (SupplierMasterPojo) selectEvent
				.getObject();
		SupplierUsersListMBean supplierUsersListMBean = (SupplierUsersListMBean) faceContext
				.getApplication().evaluateExpressionGet(faceContext,
						"#{supplierUsersListMBean}",
						SupplierUsersListMBean.class);
		try {
			supplierUsersListMBean.loadAllUsersBySupplier(supplierMasterPojo);
		} catch (BusinessException e) {
			log.error(e);
		}
		return "showSupplierUsers";
	}
}
